package com.hellobike.haxing.modules.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.common.annotation.AutoLog;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.product.entity.PromotionGroup;
import com.hellobike.haxing.modules.product.service.IPromotionGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 拼团商品
 * @Author: haxing-boot
 * @Date: 2020-01-29
 * @Version: V1.0
 */
@RestController
@RequestMapping("/product/promotionGroupNoJoin")
@Slf4j
public class PromotionGroupNoJoinController extends BaseController<PromotionGroup, IPromotionGroupService> {
    @Autowired
    private IPromotionGroupService promotionGroupService;


    @PostMapping(value = "/selectSkuList")
    public Result<?> selectSkuList(@RequestParam Long id, @RequestParam Long productId) {
        return promotionGroupService.selectNojoinProducts(id, productId);
    }
    @PostMapping(value = "/selectProduct")
    public Result<?> selectProduct(@RequestParam Long promotionId) {
        return promotionGroupService.selectProduct(promotionId);
    }

    /**
     * 分页列表查询
     *
     * @param promotionGroup
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "拼团商品-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(PromotionGroup promotionGroup,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
//		QueryWrapper<PromotionGroup> queryWrapper = QueryGenerator.initQueryWrapper(promotionGroup, req.getParameterMap());
        QueryWrapper<PromotionGroup> queryWrapper = new QueryWrapper<>(promotionGroup);
        queryWrapper.orderByAsc("sort_index");

        Page<PromotionGroup> page = new Page<PromotionGroup>(pageNo, pageSize);
        IPage<PromotionGroup> pageList = promotionGroupService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param promotionGroup
     * @return
     */
    @AutoLog(value = "拼团商品-添加", operateType = 2)
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody PromotionGroup promotionGroup) {
        return promotionGroupService.savePromotionGroup(promotionGroup);
    }

    /**
     * 编辑
     *
     * @param promotionGroup
     * @return
     */
    @AutoLog(value = "拼团商品-编辑", operateType = 3)
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody PromotionGroup promotionGroup) {
        return promotionGroupService.updatePromotionGroup(promotionGroup);
    }

    @GetMapping("/includeProductIds")
    public Result<?> getIncludeProductIds(@RequestParam(name = "promotionId", required = true) Long promotionId) {
        QueryWrapper<PromotionGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PromotionGroup::getPromotionId, promotionId).select(PromotionGroup::getProductId);
        List<Long> productIdList = promotionGroupService.listObjs(queryWrapper, productId -> (Long) productId);
        return Result.ok(productIdList);
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "拼团商品-删除", operateType = 4)
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) Long id) {
        promotionGroupService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "拼团商品-批量删除", operateType = 4)
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.promotionGroupService.removeByIds(Arrays.asList(ids.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList()));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) Long id) {
        PromotionGroup promotionGroup = promotionGroupService.getPromotionGroupDetail(id);
        if (promotionGroup == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(promotionGroup);
    }

    /**
     * 拼团商品上下架
     *
     * @param id
     * @param status
     * @return
     */
    @GetMapping(value = "/changeStatus")
    public Result<?> changeOnlineStatus(@RequestParam(name = "id", required = true) Long id, @RequestParam(name = "status", required = true) Integer status) {
        return promotionGroupService.changeStatus(id, status);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param promotionGroup
     */
    @AutoLog(value = "拼团商品-导出excel", operateType = 6)
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, PromotionGroup promotionGroup) {
        return super.exportXls(request, promotionGroup, PromotionGroup.class, "拼团商品");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @AutoLog(value = "拼团商品-导入excel", operateType = 5)
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, PromotionGroup.class);
    }

}
